package com.cdu.guoxinan.www.smartcom.tool;

import com.cdu.guoxinan.www.smartcom.domian.pojo.Page;
import com.cdu.guoxinan.www.smartcom.domian.vo.PageInfo.Child;
import com.cdu.guoxinan.www.smartcom.domian.vo.PageInfo.MenuInfo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class Tool {
    // 构建菜单树
    public List<MenuInfo> buildMenuTree(List<Page> pages){
        //获取所有页面类型
        List<String> pageClass = new ArrayList<>() ;
        for (Page page : pages) {
            if (!pageClass.contains(page.getPageClass())) {
                pageClass.add(page.getPageClass());
            }
        }
        List<MenuInfo> menuInfoList = new ArrayList<>();
        for(String pageClassName:pageClass){
            MenuInfo menuInfo = new MenuInfo();
            menuInfo.title = pageClassName;
            menuInfo.child = new ArrayList<>();
            for (Page page : pages){
                if (page.getPageClass().equals(pageClassName)){
                    Child child = new Child();
                    child.title = page.getPageTitle();
                    child.href = page.getPagePath();
                    child.icon = page.getPageIcon();
                    menuInfo.icon = page.getPageClassIcon();
                    menuInfo.child.add(child);
                }
            }
            menuInfoList.add(menuInfo);
        }
        return menuInfoList;
    }
}
